package iwpl.project.servlet;

import iwpl.project.dao.CustomersDAO;
import iwpl.project.dao.EmployeesDAO;
import iwpl.project.entities.Apartments;
import iwpl.project.entities.Customers;
import iwpl.project.entities.Departments;
import iwpl.project.entities.Employees;
import iwpl.project.entities.Offices;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CUSTOMER_Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CUSTOMER_Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		String cusID=request.getParameter("id");
		Customers cus=new Customers();
		CustomersDAO dao=new CustomersDAO();
		if ("delete".equalsIgnoreCase(action)) {
			EntityManagerHelper.beginTransaction();
			cus.setId(Integer.parseInt(cusID));
			dao.delete(cus);
			EntityManagerHelper.commit();
			}
		response.sendRedirect("/DepartmentManage/Manage_Customer.jsp");
		/*SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
		String action = request.getParameter("action");
		String cusID=request.getParameter("txtID");
		String fullName=request.getParameter("txtCus_Name");
		String phone=request.getParameter("txtPhone");
		String sex=request.getParameter("chkSex");
		String birthdate=request.getParameter("txtBirthDate");
		String address=request.getParameter("txtAddress");
		String apartment=request.getParameter("cboApartment");
		String IsOwner=request.getParameter("chkOwner");
		if (sex == null)
			sex = "false";
		if (sex.equalsIgnoreCase("on"))
			sex = "true";
		if (IsOwner == null)
			IsOwner = "false";
		if (IsOwner.equalsIgnoreCase("on"))
			IsOwner = "true";
		Date date = null;
		try {
			date = sdf.parse(birthdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Timestamp ts=new Timestamp(date.getTime());
		Customers cus=new Customers();
		CustomersDAO dao=new CustomersDAO();
		cus.setFullName(fullName);
		cus.setAddress(address);
		cus.setPhone(phone);
		cus.setBirthday(ts);
		cus.setSex(sex);
		cus.setHost(IsOwner);
		Apartments apartObj=new Apartments();
		apartObj.setId(Integer.parseInt(apartment));		
		cus.setApartments(apartObj);
		if(cusID==null)
		{
			EntityManagerHelper.beginTransaction();
			dao.save(cus);
			EntityManagerHelper.commit();
		}
		else if(cusID!=null)
		{
			EntityManagerHelper.beginTransaction();
			cus.setId(Integer.parseInt(cusID));
			dao.update(cus);
			EntityManagerHelper.commit();
		}
		else if ("delete".equalsIgnoreCase(action)) {
			EntityManagerHelper.beginTransaction();
			cus.setId(Integer.parseInt(cusID));
			dao.delete(cus);
			EntityManagerHelper.commit();
			}
		response.sendRedirect("/DepartmentManage/Manage_Customer.jsp");*/
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
		//String action = request.getParameter("action");
		String cusID=request.getParameter("txtID");
		String fullName=request.getParameter("txtCus_Name");
		String phone=request.getParameter("txtPhone");
		String sex=request.getParameter("chkSex");
		String birthdate=request.getParameter("txtBirthDate");
		String address=request.getParameter("txtAddress");
		String apartment=request.getParameter("cboApartment");
		String IsOwner=request.getParameter("chkOwner");
		if (sex == null)
			sex = "false";
		if (sex.equalsIgnoreCase("on"))
			sex = "true";
		if (IsOwner == null)
			IsOwner = "false";
		if (IsOwner.equalsIgnoreCase("on"))
			IsOwner = "true";
		Date date = null;
		try {
			date = sdf.parse(birthdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Timestamp ts=new Timestamp(date.getTime());
		Customers cus=new Customers();
		CustomersDAO dao=new CustomersDAO();
		cus.setFullName(fullName);
		cus.setAddress(address);
		cus.setPhone(phone);
		cus.setBirthday(ts);
		cus.setSex(sex);
		cus.setHost(IsOwner);
		Apartments apartObj=new Apartments();
		apartObj.setId(Integer.parseInt(apartment));		
		cus.setApartments(apartObj);		
		if(cusID==null)
		{
			EntityManagerHelper.beginTransaction();
			dao.save(cus);
			EntityManagerHelper.commit();
		}
		else if(cusID!=null)
		{
			EntityManagerHelper.beginTransaction();
			cus.setId(Integer.parseInt(cusID));
			dao.update(cus);
			EntityManagerHelper.commit();
		}
		/*else if ("delete".equalsIgnoreCase(action)) {
			EntityManagerHelper.beginTransaction();
			cus.setId(Integer.parseInt(cusID));
			dao.delete(cus);
			EntityManagerHelper.commit();
			}*/
		response.sendRedirect("/DepartmentManage/Manage_Customer.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
